const express = require('express')
const app = express() 
const USERNAME_DOES_NOT_EXISTS = 'USERNAME_DOES_NOT_EXISTS'
const USER_ALREADY_EXISTS = 'USER_ALREADY_EXISTS' 
app.post('/login', (req, res, next) =>{
  const isLogin = true
  if(isLogin) {
    res.json('user login success')
  }else{
    // res.type(400)
    // res.json('username does not exits')
    next(new Error(USERNAME_DOES_NOT_EXISTS))
  }
})

app.post('/register', (req, res, next) =>{
  const isExists = false
  if(isExists) {
    res.json('user register success')
  }else{
    // res.type(400)
    // res.json('username already exits')
    next(new Error(USER_ALREADY_EXISTS))
  }
})
app.use((err, req, res, next) =>{
  let status = 400
  let message = ''
  switch(err.message){
    case USERNAME_DOES_NOT_EXISTS:
      message = 'username does not exists'
      break
    case USER_ALREADY_EXISTS:
      message = 'user already exists'
    default:
      message:'not found'
  }
  res.status(status)
  res.json({
    errCode: status,
    errMessage: message
  })
})

app.listen(8000, ()=>{
  console.log('服务器启动成功')
})
